<?php 

require('../qcubed.inc.php');

require('panels/PersonPersonPanel.class.php');
require('panels/PersonWorkPanel.class.php');
require('panels/PersonHomePanel.class.php');
require('panels/PersonAddressPanel.class.php');
require('panels/PersonInfoPanel.class.php');

class PersonForm extends QForm {
        // Local instance of the data and meta
        protected $mctPerson;
        protected $mctAddress;
        protected $objPerson;
        protected $objAddress;

        protected $authSys;

        //Tabs
        protected $tabMain;
        protected $pnlSecPerson;
        protected $pnlSecHome;
        protected $pnlSecWork;
        protected $pnlSecAddress;
        protected $pnlSecInfo;
        
        //Buttons
        protected $btnSave;
        protected $btnCancel;

        protected function Form_Run() {
            $this->authSys = new Perm();
            $this->authSys->IsAuthenticated(true);
        }

	protected function Form_Create() {
            $this->mctPerson = PersonMetaControl::CreateFromPathInfo($this);
            $this->objPerson= $this->mctPerson->__get("Person");

            //Check perm and redirect if not access
            $this->authSys->Check_Person_Perm($this->objPerson->Id);

            
            if ( $this->objPerson->AddressId)
                $this->mctAddress = AddressMetaControl::Create($this, $this->objPerson->AddressId, QMetaControlCreateType::EditOnly);
            else
                $this->mctAddress = AddressMetaControl::Create($this, null);
            
            $this->tabMain = new Jtab("MainTab");
            
            $this->pnlSecPerson = new QePanel($this);
            $this->pnlSecPerson = new PersonPersonPanel($this->pnlSecPerson, $this->mctPerson, null);
            $this->tabMain->AddTab(QApplication::Translate("Person") , $this->pnlSecPerson);

            $this->pnlSecWork = new QePanel($this);
            $this->pnlSecWork = new PersonWorkPanel($this->pnlSecWork, $this->mctPerson,$this->mctAddress, null);
            $this->tabMain->AddTab(QApplication::Translate("Work") , $this->pnlSecWork);

            $this->pnlSecHome = new QePanel($this);
            $this->pnlSecHome = new PersonHomePanel($this->pnlSecHome,$this->mctAddress, null);
            $this->tabMain->AddTab(QApplication::Translate("Home") , $this->pnlSecHome);

            $this->pnlSecAddress = new QePanel($this);
            $this->pnlSecAddress = new PersonAddressPanel($this->pnlSecAddress,$this->mctAddress, null);
            $this->tabMain->AddTab(QApplication::Translate("Address") , $this->pnlSecAddress);

            $this->pnlSecInfo = new QePanel($this);
            $this->pnlSecInfo = new PersonInfoPanel($this->pnlSecInfo,$this->mctPerson, $this->mctAddress);
            $this->tabMain->AddTab(QApplication::Translate("Records Info") , $this->pnlSecInfo);

            //$this->txtNotes = $this->mctAddress->txtNotes_Create();

            // Create Buttons and Actions on this Form
            $this->btnSave = new QButton($this);
            $this->btnSave->Text = QApplication::Translate('Save');
            $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
            $this->btnSave->CausesValidation = true;

            $this->btnCancel = new QButton($this);
            $this->btnCancel->Text = QApplication::Translate('Cancel');
            $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));
	}

        protected function Form_Validate() {
                // By default, we report that Custom Validations passed
                $blnToReturn = true;
                $blnFocused = false;
                if ( $this->pnlSecPerson->Validate() ){
                    $blnToReturn=false;
                    if (!$blnFocused){ $this->tabMain->ChangeTabById(0);$blnFocused=true;}
                }
                if ( $this->pnlSecWork->Validate() ){
                    $blnToReturn=false;
                    if (!$blnFocused){ $this->tabMain->ChangeTabById(1);$blnFocused=true;}
                }
                if ( $this->pnlSecHome->Validate() ){
                    $blnToReturn=false;
                    if (!$blnFocused){ $this->tabMain->ChangeTabById(2);$blnFocused=true;}
                }
                return $blnToReturn;
        }
             
        // Button Event Handlers
        protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
                  $this->mctAddress->SaveAddress();
                  $this->mctPerson->SavePerson();

                  $this->objAddress = $this->mctAddress->__get("Address");
                  $this->objAddress->AssociatePerson($this->objPerson);
                  
                  $this->objAddress->Save();

                  $this->RedirectToListPage();
        }
        
        protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {
			$this->RedirectToListPage();
	}

        // Other Methods
        protected function RedirectToListPage() {
                QApplication::Redirect(__PERSON__ . '/person_list.php');
        }

}
PersonForm::Run('PersonForm','tpl/person_edit.tpl.php');

?>
